/**
 * This project create for studying propose at BUPT University. You can change or reuse this project anywhere
 */

package edu.bupt.ooad.library;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author Hamed Abdollahpour
 */
public abstract class Patron {

    public enum PetronType {
        TEACHER, STUDENT
    };

    private List<Borrow> borrows = new ArrayList<Borrow>();

    private int id;

    private String firstname;

    private String lastname;

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getFirstname() {
        return firstname;
    }

    public void setFirstname(String firstname) {
        this.firstname = firstname;
    }

    public String getLastname() {
        return lastname;
    }

    public void setLastname(String lastname) {
        this.lastname = lastname;
    }

    public boolean barrowBook(Book book) {
        Borrow borrow = new Borrow();
        borrow.setBook(book);
        borrow.setDate(new Date());
        borrow.setPetron(this);

        book.setAvailable(false);

        borrows.add(borrow);

        return true;
    }

    public boolean returnBook(Book book) {
        for (int i = borrows.size() - 1; i >= 0; i--) {
            Borrow borrow = borrows.get(i);
            if (borrow.getBook().getBookId() == book.getBookId()) {
                borrows.remove(borrow);
                book.setAvailable(true);

                return true;
            }
        }
        return false;
    }

    public List<Borrow> getBorrows() {
        return borrows;
    }

    public abstract PetronType getType();

}
